package com.csyf.manager.controller.system;

import com.csyf.manager.common.CommonConst;
import com.csyf.manager.entity.SysChannelAttr;
import com.csyf.manager.service.SysChannelAttrBiz;
import com.csyf.manager.utils.ResultEntity;
import com.csyf.manager.utils.ResultUtli;
import com.csyf.manager.utils.page.JqGridVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: casper
 * @Description: 产品配置
 * @Date: Created in 2019/3/8 16:07
 */
@Controller
@RequestMapping("/channelAttr")
public class SysChannelAttrController {

    @Autowired
    private SysChannelAttrBiz sysChannelAttrBiz;


    /**
     * 产品配置列表
     * @param vo
     * @param page
     * @param rows
     * @return
     */
    @PostMapping(value = "list")
    @ResponseBody
    public JqGridVo list(SysChannelAttr vo, Integer page, Integer rows) {
        return sysChannelAttrBiz.queryChannelAttrList(vo, page, rows);
    }

    /**
     * 新增产品配置
     * @param channelAttrForm
     * @return
     */
    @ResponseBody
    @PostMapping(value = "add")
    public ResultEntity add(SysChannelAttr channelAttrForm) {
        try {
            sysChannelAttrBiz.saveChannelAttr(channelAttrForm);
            return ResultUtli.DEFAULT_SUCCESS("添加产品配置成功", null);
        } catch (Exception e) {
            return ResultUtli.DEFAULT_ERROR("添加产品配置失败"+ e.getMessage());
        }
    }

    /**
     * 跳转产品配置页面
     * @param model
     * @param service
     * @return
     */
    @GetMapping("edit")
    public String editChannelAttr(Model model, String service) {
        SysChannelAttr channelAttr = sysChannelAttrBiz.findTradeByService(service);
        model.addAttribute("channelAttr", channelAttr);
        return "channelAttr/channelAttrDetail";
    }

    /**
     * 编辑产品配置
     * @param channelAttrForm
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public ResultEntity edit(SysChannelAttr channelAttrForm) {
        int num = sysChannelAttrBiz.updateByService(channelAttrForm);
        if (num > 0) {
            return ResultUtli.DEFAULT_SUCCESS("编辑产品配置成功", null);
        } else {
            return ResultUtli.DEFAULT_ERROR("编辑产品配置失败");
        }
    }

    /**
     * 删除产品配置
     * @param service
     * @return
     */
    @GetMapping("/delete")
    @ResponseBody
    public ResultEntity deleteChannelAttr(String service) {
        int num = sysChannelAttrBiz.deleteByService(service);
        if (num > 0) {
            return ResultUtli.DEFAULT_SUCCESS("删除产品成功", null);
        } else {
            return ResultUtli.DEFAULT_ERROR("删除产品失败");
        }
    }



}
